<template>
  <div class="avue-contail"
       :class="{'avue--collapse':isCollapse}">
    <div class="avue-header">
      <!-- 顶部导航栏 -->
      <top/>
    </div>

    <div class="avue-layout">
      <div class="avue-left">
        <!-- 左侧导航栏 -->
        <sidebar/>
      </div>
      <div class="avue-main">
        <!-- 顶部标签卡 -->
        <tags/>
        <!-- 主体视图层 -->
        <el-scrollbar style="height:100%">
          <keep-alive>
            <router-view class="avue-view"
                         v-if="$route.meta.keepAlive"/>
          </keep-alive>
          <router-view class="avue-view"
                       v-if="!$route.meta.keepAlive"/>
        </el-scrollbar>

      </div>
    </div>
    <!-- <el-footer class="avue-footer">
      <img src="/svg/logo.svg"
           alt=""
           class="logo">
      <p class="copyright">© 2018 Avue designed by smallwei</p>
    </el-footer> -->
    <div class="avue-shade"
         @click="showCollapse"></div>
  </div>
</template>

<script>
  import {mapGetters} from "vuex";
  import tags from "./tags";
  import top from "./top/";
  import sidebar from "./sidebar/";
  import admin from "@/util/admin";
  import {getStore} from "../../util/store";
  import {calcDate} from "../../util/date";
  import {validatenull} from "../../util/validate";
  import {getToken, removeToken, setToken} from "../../util/auth";

  export default {
    components: {
      top,
      tags,
      sidebar
    },
    name: "index",
    data() {
      return {
        //刷新token锁
        refreshLock: false,
        //刷新token的时间
        refreshTime: "",
      };
    },
    created() {
      //实时检测刷新token
      this.refreshToken();
    },
    beforeDestroy() {
      console.log("this.refreshTime")
      console.log(this.refreshTime)
      if (this.refreshTime != null) {
        clearInterval(this.refreshTime);
      }
    },
    mounted() {
      this.init();
    },
    computed: {
      ...mapGetters(["isLock", "isCollapse", "website"])
    },
    props: [],
    methods: {
      showCollapse() {
        this.$store.commit("SET_COLLAPSE");
      },
      // 屏幕检测
      init() {
        this.$store.commit("SET_SCREEN", admin.getScreen());
        window.onresize = () => {
          setTimeout(() => {
            this.$store.commit("SET_SCREEN", admin.getScreen());
          }, 0);
        };
      },

      // 10分钟检测一次token。目前使用后端的自动续签，前台不用刷新token
      refreshToken() {
        this.refreshTime = setInterval(() => {
          const token = getStore({
            name: "token",
            debug: true
          });
          const date = calcDate(token.datetime, new Date().getTime());
          if (validatenull(date)) return;
          console.log(JSON.stringify(date));
          // 目前的实现，这段代码(date.seconds >= this.website.tokenTime)永远不会满足。只要tokenTime大于这里的定时器时间，目前是10*1000，即10秒
          // 当生成的token的时间距今超过100分钟，就重新保存token，相当于重置时间
          if ((date.seconds > this.website.tokenTime) && !this.refreshLock) {
            this.refreshLock = true;
            this.$store
              .dispatch("RenewToken")
              .then(() => {
                console.log("then");
                let token = getToken();
                // console.log("token="+token);
                this.$store.commit('SET_TOKEN', token)
                this.refreshLock = false;
              })
              .catch(() => {
                console.log("catch");
                this.refreshLock = false;
              });
          }
        }, 10 * 1000);
      }
    }
  };
</script>
